<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 会吃草的鱼 <783188184@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Shop\Api\ProductMemberPriceApi;
use Think\Controller;
use Admin\Api\FreightTemplateApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductImageApi;
use Shop\Api\ProductSkuApi;
use Shop\Api\ShoppingCartApi;

class ShoppingCartController extends HomeController {

    /*
     * 购物车主页面
     * */
    public function index(){
        $uid = $this->userinfo['id'];//从session中获取
        $vip = $this->role[''];//session中获取会员等级
        $page_no = I('page_no',0);
        $page_size = I('page_size',10);
        $map = array(
            'uid' => $uid,
        );

        $page = array('curpage'=>$page_no,'size'=>$page_size); //分页查询
        $order="store_id desc,update_time desc";
        $result=apiCall(ShoppingCartApi::QUERY,array($map,$page,$order));

        if($result['status']){
            $list = $result['info']['list'];
            if(is_array($list)){
                foreach($list as &$vo){
                    $vo['icon_url'] = getImageUrl($vo['icon_url']);
                    $sku_id = apiCall(ProductSkuApi::GET_INFO,array(array('product_id' => $vo['p_id'],'sku_id' => $vo['sku_id'])));

                    if($sku_id['status']){
                        $vo['quantity'] = $sku_id['info']['quantity'];
                        $vo['sku_desc'] = $sku_id['info']['sku_desc'];
                        $vo['vipprice'] = $this->role['discount']*$sku_id['info']['price'];
                        $vo['points'] = $this->role['integral_multiple']*$sku_id['info']['price'];
                    }

                }
            }
            $this->assign('list',$list);
        }

        $this->display();
    }


    /*
     * 加入购物车ajax请求
     * */
    public function into_cart(){

        $uid = $this->userinfo['id'];
        $p_id = I('pid');
        $count = I('count');
        $sku = I('sku');
        $store_id = 0;
        $name = "";
        $template_id = 0;
        $weight = 0.0;

        $main_img = "";

        $sku_id = "";
        $sku_desc = "";
        $icon_url = "";
        $price = 0.0;
        $ori_price = 0.0;

        $id = 0;
        $ori_count = 0;

        if($count < 0){
            $this ->apiReturnErr('商品数量有误');
        }

        $map = array(
            'id' => $p_id,
        );

        $result = apiCall(ProductApi::GET_INFO,array($map));

        if($result['status']){
            $store_id = $result['info']['store_id'];
            $name = $result['info']['name'];
            $template_id = $result['info']['template_id'];
            $weight = $result['info']['weight'];
        }else{
            $this -> apiReturnErr($result['info']);
        }

        $map = array(
            'id' => $result['info']['template_id'],
        );
       /* $result = apiCall(FreightTemplateApi::GET_INFO,array($map));
        if($result['status']){
            if(is_null($result['info'])){
                $this -> apiReturnErr('请联系商家检查商品运费模板');
            }
        }else{
            $this -> apiReturnErr($result['info']);
        }*/

        $map = array(
            'sku_id' => $sku,
            'product_id' => $p_id
        );

        $result = apiCall(ProductSkuApi::GET_INFO,array($map));
        if($result['status']){
            if($result['info']['product_id'] == $p_id){
                $psku_id = $result['info']['id'];
                $sku_id = $result['info']['sku_id'];
                $sku_desc = $result['info']['sku_desc'];
                $price = $result['info']['price'];
                $ori_price = $result['info']['ori_price'];
                $icon_url = $result['info']['icon_url'];
                //看看商品的库存是否足够
                if($result['info']['quantity'] < $count){
                    $this -> apiReturnErr('库存不足');
                }
            }else{
                $this -> apiReturnErr("商品规格有误");
            }

        }else{
            $this -> apiReturnErr($result['info']);
        }


        if($icon_url == ""){
            $map = array(
                'pid' => $p_id,
                'type'=>6015,//主图
            );
            $result = apiCall(ProductImageApi::GET_INFO,array($map));
            if($result['status']){
                $icon_url = $result['info']['img_id'];
            }else{
                $this -> apiReturnErr($result['info']);
            }
        }


        //检查购物车中是否已经存有该商品该规格的信息
        $map = array(
            'uid' => $uid,
            'p_id' => $p_id,
            'psku_id' => $psku_id,
        );
        $result = apiCall(ShoppingCartApi::GET_INFO,array($map));
        if($result['status']){
            if(!is_null($result['info'])){
                $id = $result['info']['id'];
                $ori_count = $result['info']['count'];
            }
        }else{
            $this -> apiReturnErr($result['info']);
        }

        $count = $count + $ori_count;

        $entity = array(
            'uid' => $uid,
            'store_id' => $store_id,
            'p_id' => $p_id,
            'sku_id' => $sku_id,
            'sku_desc' => $sku_desc,
            'icon_url' => $icon_url,
            'count' => $count,
            'name' => $name,
            'express' => 0.0,
            'template_id' => $template_id,
            'price' => $price,
            'ori_price' => $ori_price,
            'psku_id' => $psku_id,
            'weight' => $weight,
            'taxRate' => 0.0,
        );

        if($id == 0){
            $result = apiCall(ShoppingCartApi::ADD,array($entity));
        }else{
            $result = apiCall(ShoppingCartApi::SAVE_BY_ID,array($id,$entity));
        }

        if($result['status']){
            if($id == 0){
                $this -> apiReturnSuc($result['info']);
            }else{
                $this -> apiReturnSuc($id);
            }
        }else{
            $this -> apiReturnErr($result['info']);
        }
    }

    /*
    * 异步根据规格获得价格
    * */
    public function  ajax_getvipprice(){
        $p_id = I('pid');//产品ID
        $sku = I('sku');
        $map = array(
            'sku_id' => $sku,
            'product_id' => $p_id
        );
        $result = apiCall(ProductSkuApi::GET_INFO,array($map));
        if($result['status']){
            $info['price'] = ceil($result['info']['price']*$this->role['discount']);
            $info['points'] = ceil($result['info']['price']*$this->role['discount']*$this->role['integral_multiple']);
            $this->apiReturnSuc($info);
        }
    }

    /*
     * 删除购物车
     * */
    public function delete(){
        $id = I('id');
        $map = array(
            'id'=>$id,
        );
        $result = apiCall(ShoppingCartApi::DELETE,array($map));
        if($result['status']){
            $this->apiReturnSuc('删除成功');
        }else{
            $this->apiReturnErr('删除失败');
        }
    }


}